import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import BaseAlarm from '@main/entities/service/admin/base/base-alarm.entity';
import { ApiException } from '@main/common/exceptions/api.exception';
import { SysOperationLogService } from '../../system/operationLog/operationLog.service';

@Injectable()
export class BaseAlarmService {
  constructor(
    @InjectRepository(BaseAlarm)
    private alarmRepository: Repository<BaseAlarm>,
    private operationLogService: SysOperationLogService,
  ) {}

  async list(data) {
    if (data) {
      const { machineCode } = data;

      return await this.alarmRepository.find({
        where: {
          machineCode
        }
      });
    };

    return await this.alarmRepository.find();
  }

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { alarmName, alarmCode, machineCode, alarmStatusCode } = entity;

    const where = {
      alarmName: Like(`%${alarmName}%`),
      alarmCode: Like(`%${alarmCode}%`),
    };

    if (machineCode) {
      where['machineCode'] = machineCode;
    }

    if (alarmStatusCode) {
      where['alarmStatusCode'] = alarmStatusCode;
    }

    return await this.alarmRepository.findAndCount({
      where,
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const alarm = await this.alarmRepository.insert({
      ...data,
      createName: user.nickName,
      createUserName: user.userName,
      updateName: user.nickName,
      updateUserName: user.userName,
    });
    databaseOperationLogList.push({
      type: 'add',
      dataType: 'obj',
      database: 'BaseAlarm',
      checkOnlyKeyList: [],
      data: alarm,
      whereObj: null,
      remark: `BaseAlarm表新增数据`,
    });
    await this.operationLogService.add(
      {
        type: 'add',
        msg: `报警管理模块新增报警信息`,
        databaseOperationLogList,
        remark: `新增报警信息${data.alarmName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 更新
   */
  async update(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const { id, alarmName, remark } = data;

    await this.alarmRepository.update(
      { id },
      { alarmName, remark, updateName: user.nickName, updateUserName: user.userName }
    );
    databaseOperationLogList.push({
      type: 'update',
      dataType: 'obj',
      database: 'BaseAlarm',
      checkOnlyKeyList: [],
      data: { alarmName, remark, updateName: user.nickName, updateUserName: user.userName },
      whereObj: { id },
      remark: `BaseAlarm表更新报警信息`,
    });
    await this.operationLogService.add(
      {
        type: 'update',
        msg: `报警管理模块更新报警信息`,
        databaseOperationLogList,
        remark: `更新报警信息${data.alarmName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 删除
   */
  async delete(idList: number[], user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    await this.alarmRepository.delete(idList);
    databaseOperationLogList.push({
      type: 'delete',
      dataType: 'array',
      database: 'BaseAlarm',
      checkOnlyKeyList: [],
      data: null,
      whereObj: idList,
      remark: `BaseAlarm表删除报警信息`,
    });
    await this.operationLogService.add(
      {
        type: 'delete',
        msg: `报警管理模块删除报警信息`,
        databaseOperationLogList,
        remark: `删除报警信息`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  async isExistKey(data) {
    const { alarmCode } = data;
    const result = await this.alarmRepository.findOne({ where: { alarmCode } });

    if (result) throw new ApiException(10048);
  }
}
